package tasktracker.core.abstraction;

import java.util.concurrent.atomic.AtomicInteger;

import tasktracker.abstraction.ITaskTrackerCharacteristics;
import tasktracker.abstraction.TaskTrackerCharacteristics;

public class TaskTrackerProperties implements ITaskTrackerCharacteristics {

	private boolean cancellationSupported;
	private int maxConcurrentSize;
	private boolean taskRestoreSupported;

	private AtomicInteger currentConcurrentSize;

	public TaskTrackerProperties(ITaskTrackerCharacteristics characteristics) {
		if (characteristics == null)
			characteristics = new TaskTrackerCharacteristics();
		this.cancellationSupported = characteristics.isCancellationSupported();
		this.maxConcurrentSize = characteristics.getMaxConcurrentSize();
		this.taskRestoreSupported = characteristics.isTaskRestoreSupported();

		this.currentConcurrentSize = new AtomicInteger();
	}

	@Override
	public boolean isCancellationSupported() {
		return this.cancellationSupported;
	}

	@Override
	public int getMaxConcurrentSize() {
		return this.maxConcurrentSize;
	}

	@Override
	public boolean isTaskRestoreSupported() {
		return this.taskRestoreSupported;
	}

	public int incrementConcurrent() {
		return currentConcurrentSize.incrementAndGet();
	}

	public int decrementConcurrent() {
		return currentConcurrentSize.decrementAndGet();
	}

	public int getCurrentConcurrentSize() {
		return currentConcurrentSize.get();
	}

	public int getRemainingConcurrentSize() {
		if (maxConcurrentSize < 0)
			return -1;
		return maxConcurrentSize - currentConcurrentSize.get();
	}
}
